package com.nbjtjc.safe.service.safe;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.safe.*;
import com.nbjtjc.safe.model.safe.*;
import com.nbjtjc.safe.util.MyShiroUtils;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RiskPointServiceImpl {


    @Autowired
    private RiskPointMapper riskPointMapper;

    @Autowired
    private RiskPointItemMapper riskPointItemMapper;

    @Autowired
    private RiskPointItemServiceImpl riskPointItemService;

    @Autowired
    private RiskMarkItemMapper riskMarkItemMapper;

    @Autowired
    private SafeBasisClassifyMapper safeBasisClassifyMapper;

    public Result insertSysDatabase(int riskPointId){
        Result result=new Result();

        return result;
    }

    public Result find(RiskPoint riskPoint) {
        Result result = new Result();
        //todo 修改前端pagenum
        getDCode(riskPoint);
        if(StringUtils.isEmpty(riskPoint.getKeyword())){
            riskPoint.setKeyword(null);
        }
        PageHelper.startPage(riskPoint.getPageCurrent(), riskPoint.getPageSize());
        List<RiskPoint> riskPoints = riskPointMapper.search(riskPoint);
        result.setData(new PageInfo<>(riskPoints));
        return result;
    }

    /**
     * dCode 风险点的分类，有上下级，获取当前级下面的所有风险点的dCOde
     */
    public void getDCode(RiskPoint riskPoint){
        if(StringUtils.isNotEmpty(riskPoint.getPid())){
            List<String> dCodeList=safeBasisClassifyMapper.findByPid(riskPoint.getPid(),1);
            riskPoint.getdCodeList().addAll(dCodeList);
        }
        if(StringUtils.isNotEmpty(riskPoint.getdCode())){
            riskPoint.getdCodeList().add(riskPoint.getdCode());
        }
    }

    @Transactional
    public Result copyRiskPointFromSys(Map<String,Object> params){
        Result result = new Result();
        List<Integer> riskPointIds= (List<Integer>) params.get("ids");
        Integer enterpriseId= (Integer) params.get("enterpriseId");
        List<RiskPoint> riskPointList=riskPointMapper.findByIds(riskPointIds);
        riskPointList.forEach(riskPoint -> {
            List<RiskPointItem> itemList=riskPointItemMapper.findByRiskPointId(riskPoint.getId());
            riskPointItemService.compareIconUrl(itemList);
            RiskPoint newRiskPoint=new RiskPoint();
            BeanUtil.copyProperties(riskPoint,newRiskPoint);
            newRiskPoint.setId(null);
            newRiskPoint.setEnterpriseId(enterpriseId);
            newRiskPoint.setEditUser("");
            newRiskPoint.setEditUserId(0);
            newRiskPoint.setEditTime(null);
            newRiskPoint.setCreateTime(new Date());
            newRiskPoint.setCreateUserId(MyShiroUtils.getSessionUserId());
            newRiskPoint.setCreateUser(MyShiroUtils.getSessionUserName());
            riskPointMapper.insert(newRiskPoint);
            List<RiskPointItem> newItemList=new ArrayList<>();
            itemList.forEach(item->{
                RiskPointItem newItem=new RiskPointItem();
                BeanUtil.copyProperties(item,newItem);
                newItem.setId(null);
                newItem.setCreateUserId(MyShiroUtils.getSessionUserId());
                newItem.setCreateTime(new Date());
                newItem.setEditUserId(0);
                newItem.setEditTime(null);
                newItem.setRiskPointId(newRiskPoint.getId());
                newItem.setRiskMarkItemList(item.getRiskMarkItemList());
                newItemList.add(newItem);
            });
            if(CollectionUtil.isNotEmpty(newItemList)){
                //插入风险点辨识记录
                riskPointItemMapper.insertList(newItemList);
            }
            //插入风险点图片标识的数据
            newItemList.forEach(pointItem -> {
                if(CollectionUtil.isNotEmpty(pointItem.getRiskMarkItemList())){
                    List<RiskMarkItem> riskMarkItems=pointItem.getRiskMarkItemList();
                    riskMarkItems.forEach(riskMarkItem -> {
                        riskMarkItem.setItemId(pointItem.getId());
                    });
                    riskMarkItemMapper.insertList(riskMarkItems);
                }
            });
        });
        return result;
    }


    public Result add(RiskPoint riskPoint) {
        Result result = new Result();
        riskPoint.setCreateUserId(MyShiroUtils.getSessionUserId());
        riskPoint.setCreateUser(MyShiroUtils.getSessionUserName());
        riskPoint.setCreateTime(new Date());
        int order=riskPointMapper.selectMaxOrder(riskPoint.getEnterpriseId());
        riskPoint.setpOrder(order+1);
        riskPointMapper.insertSelective(riskPoint);
        result.setData(riskPoint);
        return result;
    }

    @Transactional
    public Result delete(RiskPoint riskPoint) {
        Result result = new Result();
        riskPointMapper.deleteByPrimaryKey(riskPoint.getId());
        riskPointItemMapper.deleteByRiskPointId(riskPoint.getId());
        return result;
    }

    @Transactional
    public Result deleteBatch(List<Integer> riskPointIds){
        Result result = new Result();
        if(CollectionUtil.isNotEmpty(riskPointIds)){
            riskPointIds.forEach(pointId->{
                riskPointMapper.deleteByPrimaryKey(pointId);
                riskPointItemMapper.deleteByRiskPointId(pointId);
            });
        }
        return result;
    }

    @Transactional
    public Result update(RiskPoint riskPoint) {
        Result result = new Result();
        riskPoint.setEditUserId(MyShiroUtils.getSessionUserId());
        riskPoint.setEditUser(MyShiroUtils.getSessionUserName());
        riskPoint.setEditTime(new Date());
        riskPointMapper.updateByPrimaryKeySelective(riskPoint);
        result.setData(riskPoint);
        return result;
    }

}
